package com.hunau.iot.server.schedule;

import com.hunau.common.utils.RedisUtil;
import com.hunau.iot.server.utils.SpringContextUtils;
import com.hunau.system.domain.TbEnergy;
import com.hunau.system.domain.TbEnvironment;
import com.hunau.system.domain.TbIoterminal;
import com.hunau.system.service.impl.TbEnergyServiceImpl;
import com.hunau.system.service.impl.TbEnvironmentServiceImpl;
import com.hunau.system.service.impl.TbIoterminalServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

/**
 * @program: ruoyi
 * @description: redis中终端数据自动保存
 * @author: Mr.Liu
 * @create: 2020-03-24 15:24
 **/
@Component
public class redisDataSave {
    private static final Logger logger = LoggerFactory.getLogger(redisDataSave.class);
    private static TbEnvironmentServiceImpl tbEnvironmentService = null;
    private static TbIoterminalServiceImpl tbIoterminalService =null;
    private static TbEnergyServiceImpl tbEnergyService = null;
    private static RedisUtil redisUtil =null;

    /**
     * 终端实现，下线的处理
     * 0.1s一次
     */
    @Scheduled(fixedRate = 100)
    public void autoSaveRedisIOT(){
        if(redisUtil==null || tbEnvironmentService==null || tbIoterminalService==null||tbEnergyService==null){
            try {
                redisUtil = (RedisUtil) SpringContextUtils.getBeanByClass(RedisUtil.class);
                tbEnvironmentService = (TbEnvironmentServiceImpl) SpringContextUtils.getBeanByClass(TbEnvironmentServiceImpl.class);
                tbIoterminalService = (TbIoterminalServiceImpl) SpringContextUtils.getBeanByClass(TbIoterminalServiceImpl.class);
                tbEnergyService = (TbEnergyServiceImpl) SpringContextUtils.getBeanByClass(TbEnergyServiceImpl.class);
            }catch (Exception e){

            }
        }else {
            //logger.info("终端实现，下线的处理1");
            Object o = redisUtil.listLiftPop("iot_ioterminal");
            if(o!=null){
                tbIoterminalService.updateTbIoterminal(redisUtil.fromJson((String) o,TbIoterminal.class));
            }
            //logger.info("终端实现，下线的处理2");
        }

    }

    /**
     * 终端环境数据的处理
     *  0.05s一次
     */
    @Scheduled(fixedRate = 50)
    public void autoSaveRedisENV(){
        if(redisUtil==null || tbEnvironmentService==null || tbIoterminalService==null||tbEnergyService==null){
            try {
                redisUtil = (RedisUtil) SpringContextUtils.getBeanByClass(RedisUtil.class);
                tbEnvironmentService = (TbEnvironmentServiceImpl) SpringContextUtils.getBeanByClass(TbEnvironmentServiceImpl.class);
                tbIoterminalService = (TbIoterminalServiceImpl) SpringContextUtils.getBeanByClass(TbIoterminalServiceImpl.class);
                tbEnergyService = (TbEnergyServiceImpl) SpringContextUtils.getBeanByClass(TbEnergyServiceImpl.class);
            }catch (Exception e){

            }
        }else {
            //logger.info("终端环境数据的处理1");
            Object o = redisUtil.listLiftPop("iot_environment");
            if(o!=null){
                tbEnvironmentService.insertTbEnvironment(redisUtil.fromJson((String) o,TbEnvironment.class));
            }
            //logger.info("终端环境数据的处理2");
        }
    }
    /**
     * 终端能源数据的处理
     *  0.05s一次
     */
    @Scheduled(fixedRate = 50)
    public void autoSaveRedisEnergy(){
        if(redisUtil==null || tbEnvironmentService==null || tbIoterminalService==null||tbEnergyService==null){
            try {
                redisUtil = (RedisUtil) SpringContextUtils.getBeanByClass(RedisUtil.class);
                tbEnvironmentService = (TbEnvironmentServiceImpl) SpringContextUtils.getBeanByClass(TbEnvironmentServiceImpl.class);
                tbIoterminalService = (TbIoterminalServiceImpl) SpringContextUtils.getBeanByClass(TbIoterminalServiceImpl.class);
                tbEnergyService = (TbEnergyServiceImpl) SpringContextUtils.getBeanByClass(TbEnergyServiceImpl.class);
            }catch (Exception e){

            }
        }else {
            //logger.info("终端环境数据的处理1");
            Object o = redisUtil.listLiftPop("iot_energy");
            if(o!=null){
                tbEnergyService.insertTbEnergy(redisUtil.fromJson((String) o, TbEnergy.class));
            }
            //logger.info("终端环境数据的处理2");
        }
    }
}
